package cn.devzyh.controller;

import cn.devzyh.domain.Menu;
import cn.devzyh.domain.MenuVo;
import cn.devzyh.domain.ResultVo;
import cn.devzyh.service.MenuService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    // 查询菜单列表
    @GetMapping("/findAllMenu")
    public ResultVo findAllMenu(MenuVo menuVo) {

        PageInfo<Menu> allMenu = menuService.findAllMenu(menuVo);

        return new ResultVo(true, 200, "响应成功", allMenu);
    }

    // 回显菜单信息
    @GetMapping("/findMenuInfoById")
    public ResultVo findMenuInfoById(@RequestParam(value = "id", required = true) Integer id) {

        Map<String, Object> map = menuService.findMenuInfoById(id);

        return new ResultVo(true, 200, "响应成功", map);
    }

    // 添加&修改菜单信息
    @PostMapping("/saveOrUpdateMenu")
    public ResultVo saveOrUpdateMenu(@RequestBody Menu menu) {

        ResultVo res = new ResultVo(true, 200, "", null);
        if (null == menu.getId()) {

            menuService.saveMenu(menu);
            res.setMessage("新增菜单信息成功");
        } else {

            menuService.updateMenu(menu);
            res.setMessage("修改菜单信息成功");
        }

        return res;
    }
}
